package analysis;

import java.util.HashMap;

public class DigraphTranslator {
	private HashMap<String, String> map = new HashMap<String, String>();
	
	public DigraphTranslator()
	{
		
	}
	
	public void putCombo(String d1, String d2)
	{
		String r1 = getRev(d1);
		String r2 = getRev(d2);
		map.put(d1, d2);
		map.put(r1, r2);
		//map.put(d2, d1);
		//map.put(r2, r1);
	}
	
	public void removeCombo(String d1, String d2)
	{
		String r1 = getRev(d1);
		//String r2 = getRev(d2);
		map.remove(d1);
		map.remove(r1);
		//map.remove(r1);
		//map.remove(r2);
	}
	
	public String translate(String text)
	{
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < text.length(); i += 2)
		{
			String ss = text.substring(i, i + 2);
			String sub = map.get(ss);
			if(sub == null)
				sb.append("  ");
			else
				sb.append(sub);
		}
		return sb.toString();
	}
	
	private static String getRev(String s)
	{
		return new String(new char[] {s.charAt(1), s.charAt(0)} );
	}
}
